package com.sixdog.regulate.deviceConfig;

import com.sixdog.regulate.bean.Device;
import com.sixdog.regulate.constant.JFrameItem;
import com.sixdog.regulate.ctl.JFrameCtl;
import com.sixdog.regulate.service.DeviceService;
import com.sixdog.regulate.utils.DeviceUtils;
import com.sixdog.regulate.windows.RegulateMain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

// 刷新设备列表
@SuppressWarnings("all")
@Component
public class DeviceRefresh implements Runnable{

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private JFrameCtl ctl;



    @Autowired
    private RegulateMain main;

    @Override
    public void run() {
        // 获取headLabel
        JLabel headLabel = ctl.getLabels().get(JFrameItem.JFrameItemName.HEAD_LABEL.getName());
        // 获取serialNoList
        JList<String> data = (JList<String>) ctl.getOthers().get(JFrameItem.JFrameItemName.SERIAL_NO_LIST.getName());
        // 读取保存的信息
        try {
            System.out.println("正在读取保存的信息..............");
            Map<String, Device> deviceMap = DeviceUtils.deserializationDevices();
            deviceMap.entrySet().forEach(item -> {
                DeviceUtils.devices.put(item.getKey(),item.getValue());
            });
            System.out.println(DeviceUtils.devices);
            System.out.println("读取完毕.................");
        } catch (Exception e) {
            e.printStackTrace();
        }
        while(!main.isClosed()) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //headLabel.setText("111111");
            ArrayList<String> serials = deviceService.getDeviceSerials();
//            if(serials.size() > 0) {
//                // 移除列表中的第一个"List"字符串
//                serials.remove(0);
//                // 移除列表中最后一个""空串
//                serials.remove(serials.size() - 1);
//            }
            // 获取所有的设备列表
            if(serials.size() > 0){
                List<Device> serialNos = DeviceUtils.getDeviceInfoBySerialNos(serials.toArray(new String[serials.size()]));
                try{
                    // 序列化操作
                    DeviceUtils.serialDevices();
                }catch (Exception e){
                    e.printStackTrace();
                }
                //System.out.println("大于0.............................");
            }
            // 清空原有的数据
            data.setListData(new String[0]);
            // 更新
            String[] strings = new String[serials.size()];
            data.setListData(serials.toArray(strings));
            //System.out.println(Arrays.asList(strings));
            // 清空devices中原有的数据
            //devices.removeAll(devices);
            //System.out.println("更新完成:" + "设备序列号:" + Arrays.asList(strings));
            try {
                Thread.sleep(8000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //headLabel.setText("22222");
        }
    }
}
